
var text = ActiveDoc.SelText;
var reg_hex = /^#?((?:[0-9A-F]{3}){1,2})$/i ,
	reg_rgb = /^rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/i ;

if (text && text != "") {
	if (reg_hex.test(text)) {
		ActiveDoc.SelText = text.replace(reg_hex, function($0, $1){
			// The jScript really sucks, access a string like a Array is not feasible !
			var rgb = $1.length==3 ? ($1.charAt(0)+$1.charAt(0)+$1.charAt(1)+$1.charAt(1)+$1.charAt(2)+$1.charAt(2)) : $1;
			rgb = parseInt(rgb, 16);
			return ("rgb(" + [(rgb&0xFF0000)>>16, (rgb&0xFF00)>>8, rgb&0xFF].join(", ") + ")");
		});
	} else if (reg_rgb.test(text)) {
		ActiveDoc.SelText = text.replace(reg_rgb, function($0, $1, $2, $3){
			var r, g, b, hex;
			r = parseInt($1);
			g = parseInt($2);
			b = parseInt($3);
			if(r>255 || g>255 || b>255) return $0;
			hex = r*65536 + g*256 + b;
			hex = hex.toString(16).toUpperCase();
			hex = "000000".substr(hex.length)+hex;
			return "#" + hex;
		});
	}
}
